// @map
const express = require('express');
const router = express.Router();
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const keys = require('../../config/keys');
const passport = require('passport');
const Map = require('../../models/Map');
const open_api = require('../../controller/open_api');

// @route  GET api/map/map
// @desc   返回的请求的json数据
// @access public
router.get('/map', (req, res) => {
    res.json({ msg: 'login works' });
  });
  

// @route  Post api/map/addmap
// @desc   新增地图
// @access public
router.post('/addmap', (req, res) => {
    Map.findOne({ mid: req.body.mid }).then(map => {
        if (map) {
          return res.status(400).json('该地图已存在');
        } else {
            const newMap = {};
            if(req.body.mid){newMap.mid = req.body.mid};
            if(req.body.name){newMap.name = req.body.name};
            if(req.body.icon){newMap.icon = req.body.icon};
            if(req.body.info){newMap.info = req.body.info};
            if(req.body.type){newMap.name = req.body.type};
            if(req.body.npcs){newMap.name = req.body.npcs};
            new Map(newMap).save()
            .then(map => res.json(map))
            .catch(err => console.log(err));
        }
    });
  });

// @route  GET api/maps/getMapInfo
// @desc   return all map info
// @access Private
router.get('/getMapInfo', passport.authenticate('jwt', { session: false }),open_api.getMapInfo);
  

// @route  GET api/maps/getplayer/:id
// @desc   返回对应地图id的用户和NPC
// @access Private
router.get('/getplayer/:id', passport.authenticate('jwt', { session: false }),open_api.getPlayerInfo);
  

module.exports = router;
  